package view;

import java.awt.*;
import java.awt.geom.Rectangle2D;
import javax.swing.JSlider;
import javax.swing.plaf.metal.MetalSliderUI;
import util.PaintUtilities;

/**
 * A custom UI for painting the Seeker rather than the default bulky JSlider UI.
 */
public class SeekerUI extends MetalSliderUI {

	private static final Color THUMB_COLOR_TOP = new Color(0x882200);
	private static final Color THUMB_COLOR_BOTTOM = new Color(0xC43100);
	private static final Font THUMB_FONT = new Font("Arial", Font.BOLD, 8);

	private static final Color TRACK_COLOR = new Color(0xCC6644);

	private JSlider seeker;

	public SeekerUI(JSlider seeker) {
		this.seeker = seeker;
	}


	@Override
	public void paintThumb(Graphics g) {
		Graphics2D g2 = (Graphics2D)g;
		PaintUtilities.setUpGraphics(g2);

		//Paint the thumb background
		g2.setPaint(new GradientPaint(
			0, 0,THUMB_COLOR_TOP, 0, thumbRect.height, THUMB_COLOR_BOTTOM
		));
		g2.fill(thumbRect);

		//Paint the text centred
		g2.setPaint(Color.WHITE);
		String value = Integer.toString(seeker.getValue());
		Rectangle2D rect = THUMB_FONT.getStringBounds(value, g2.getFontRenderContext());
		g2.setFont(THUMB_FONT);
		g2.drawString(value,
			(int)(thumbRect.x + (thumbRect.width + rect.getWidth()) / 2 - rect.getWidth()),
			(int)(thumbRect.y + (thumbRect.height + rect.getHeight()) / 2) - 1
		);
	}


	@Override
	public void paintTrack(Graphics g) {
		g.setColor(TRACK_COLOR);
		//Paint up to the thumb to show a trail.
		g.fillRect(0, 0, thumbRect.x + (thumbRect.width / 2), trackRect.height - 1);
	}

}